package com.jf.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.sys.android.util.FinalUntil;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

@ContentView(R.layout.my_message)
public class MyMessage extends Activity {

	private ListView datalist = null; // 定义ListView组件
	private List<Map<String, String>> list = new ArrayList<Map<String, String>>(); // 定义显示的内容包装
	private SimpleAdapter simpleAdapter = null; // 进行数据的转换操作
	private SharedPreferences useShare;
	private String userId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);// xml视图加载的处理
		useShare = getSharedPreferences("userInfo", MODE_PRIVATE);
		userId = useShare.getString("username", "");

		this.datalist = (ListView) super.findViewById(R.id.listView1); // 取得组件
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("courierUserID", userId);
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, FinalUntil.ServerAddress + "findInstantMess",
				params, new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						Log.i("MyMessage", arg1);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						try {
							JSONArray jsonarr = new JSONArray(arg0.result);
							for (int x = 0; x < jsonarr.length(); x++) {
								JSONObject jsonobj = jsonarr.getJSONObject(x);
								Map<String, String> map = new HashMap<String, String>();
								map.put("sendName",
										jsonobj.getString("contendOrder"));
								map.put("sendcall",
										jsonobj.getString("courphone"));
								map.put("sendaddress",
										"");
								list.add(map);
							}
							simpleAdapter = new SimpleAdapter(MyMessage.this,
									list, R.layout.my_sendfilelist,
									new String[] { "sendName", "sendcall",
											"sendaddress" } // Map中的key的名称
									, new int[] { R.id.sendName, R.id.sendcall,
											R.id.sendaddress }); // 是data_list.xml中定义的组件的资源ID
							datalist.setAdapter(simpleAdapter);
							datalist.setOnItemClickListener(new OnItemClickListenerImpl()); // 单击选项
						} catch (Exception e) {
							Log.e("MyMessage", e.toString());
						}
					}
				});
	}

	private class OnItemClickListenerImpl implements OnItemClickListener {

		@SuppressWarnings("unchecked")
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Map<String, String> map = (Map<String, String>) MyMessage.this.simpleAdapter
					.getItem(position);
			String sendName = map.get("sendName");
			String sendcall = map.get("sendcall");
			Intent intent = new Intent(MyMessage.this,
					com.jf.socketMess.MyMessage.class);
			intent.putExtra("sendName", sendName);
			intent.putExtra("sendcall", sendcall);
			startActivity(intent);
			// Toast.makeText(MyMessage.this,sendName,
			// Toast.LENGTH_LONG).show();
		}
	}

	public void finishMessage(View v) {
		this.finish();
	}
}
